package com.wuxinwudai.android.app;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import com.wuxinwudai.adlg.ContainerDialog;
import com.wuxinwudai.android.util.DateTimeUtils;
import com.wuxinwudai.android.util.FileUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

/**
 * DefaultCrashReporter 类为默认的异常报告器
 * @author 吾心无待 于 2016年04月11日
 *
 */
public class DefaultCrashReporter implements CrashReporter {


    /**
     * 向应用提供方报告异常
     */
    @Override
    public void report(final Context context,final String mailTo,final String message) {
        final ContainerDialog dialog = new ContainerDialog(context);
        dialog.setTitle("异常程序异常");
        dialog.setMessage("很抱歉亲，应用程序在运行过程中出现未知错误，请您发送错误报告帮助我们加以改进，谢谢您的合作！");
        dialog.setOnConfirmClickListener(new ContainerDialog.OnConfirmClickListener() {
            @Override
            public void confirm(ContainerDialog containerDialog) {
                dialog.dismiss();
                //发送异常报告
                Intent i = new Intent(Intent.ACTION_SEND);
                //i.setType("text/plain"); //模拟器
                i.setType("message/rfc822"); //真机
                i.putExtra(Intent.EXTRA_EMAIL, new String[]{mailTo});
                i.putExtra(Intent.EXTRA_SUBJECT,  context.getApplicationInfo().loadLabel(context.getPackageManager())+"- 错误报告");
                i.putExtra(Intent.EXTRA_TEXT, message);
                context.startActivity(Intent.createChooser(i, "发送错误报告"));
                //写入本地文件

                //退出
                ActivityCollector.finishAll();
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        }).setOnCancelClickListener(new ContainerDialog.OnCancelClickListener() {
            @Override
            public void cancel(ContainerDialog containerDialog) {
                dialog.dismiss();
                //退出
                ActivityCollector.finishAll();
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
        dialog.show();
    }
    /**
     * @brief 保存错误信息到文件中
     * @param ex 异常
     */
    private void saveCrashInfoToFile(Throwable ex) {
        final StackTraceElement[] stack = ex.getStackTrace();
        final String message = ex.getMessage();
        /* 准备错误日志文件 */
        String filePath= FileUtils.getRootFilePath() + "";
        File logFile = new File(filePath);
        if (!logFile.getParentFile().exists()) {
            logFile.getParentFile().mkdirs();
        }
        /* 写入错误日志 */
        FileWriter fw = null;
        final String lineFeed = "\r\n";
        try {
            fw = new FileWriter(logFile, true);
            fw.write(DateTimeUtils.CHINESE_DATE_FORMAT.format(new Date()) + lineFeed
                    + lineFeed);
            fw.write(message + lineFeed);
            for (int i = 0; i < stack.length; i++) {
                fw.write(stack[i].toString() + lineFeed);
            }
            fw.write(lineFeed);
            fw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != fw)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}